import sqlite3
# 定义枚举类型，表示analysis表中的字段
class AnalysisType:
    EXCEL = 1
    GITEE = 2
    CI = 3
    CONFIG = 4
    INTRODUCED = 5
# 插入数据到analysis表中，默认bool类型为False
def bind_issue(conn, no):
    sql = ''' INSERT INTO analysis(no, excel, gitee, ci, config, introduced)
              VALUES(?,?,?,?,?,?) '''
    cur = conn.cursor()
    cur.execute(sql, (no, False, False, False, False, False))
    conn.commit()
# 根据AnalysisType，来更新表中添加不同的字段
def update_analysis(conn, no, analysisType, value, info):
    switcher = {
        AnalysisType.EXCEL: "excel",
        AnalysisType.GITEE: "gitee",
        AnalysisType.CI: "ci",
        AnalysisType.CONFIG: "config",
        AnalysisType.INTRODUCED: "introduced"
    }
    # 如果不存在，则先插入数据
    if not is_exist_analysis(conn, no):
        bind_issue(conn, no)
    column = switcher.get(analysisType)
    sql = ''' UPDATE analysis SET {} = ?, {}Info = ? WHERE no = ?'''.format(column, column)
    cur = conn.cursor()
    cur.execute(sql, (value, info, no))
    conn.commit()
# 判断所给定的type是否已经为true
def is_already_analyze(conn, no, analysisType):
    switcher = {
        AnalysisType.EXCEL: "excel",
        AnalysisType.GITEE: "gitee",
        AnalysisType.CI: "ci",
        AnalysisType.CONFIG: "config",
        AnalysisType.INTRODUCED: "introduced"
    }
    column = switcher.get(analysisType)
    cur = conn.cursor()
    cur.execute("SELECT {} FROM analysis WHERE no = ?".format(column), (no,))
    rows = cur.fetchall()
    if len(rows) == 0:
        return False
    return rows[0][0]
# 根据no获取analysis表中的数据
# 如果没有结论 则返回空字符串
def get_analysis(conn, no, analysisType):
    switcher = {
        AnalysisType.EXCEL: "excel",
        AnalysisType.GITEE: "gitee",
        AnalysisType.CI: "ci",
        AnalysisType.CONFIG: "config",
        AnalysisType.INTRODUCED: "introduced"
    }
    column = switcher.get(analysisType)
    cur = conn.cursor()
    cur.execute("SELECT {}Info FROM analysis WHERE no = ?".format(column), (no,))
    row = cur.fetchone()
    if row is None: 
        return ""
    return row[0]
def is_exist_analysis(conn, no):
    cur = conn.cursor()
    cur.execute("SELECT * FROM analysis WHERE no = ?", (no,))
    rows = cur.fetchall()
    return len(rows) > 0
# 获取所有的analysis表中的数据
def get_all_analysis(conn):
    cur = conn.cursor()
    cur.execute("SELECT * FROM analysis")
    rows = cur.fetchall()
    return rows